
/**
 * Project Name:cats-core-impl
 * File Name:SpringContextUtil.java
 * Package Name:com.catsic.core.utils
 * Date:2014年2月28日下午12:26:13
 * Copyright (c) 2014, oa.catsic.com All Rights Reserved.
 */
 
package com.catsic.base.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;


/**
 * ClassName: SpringContextUtil
 * Description: 实现ApplicationContextAware接口，然后他会把ApplicationContext对象传给你,获得spring容器的引用
 * date: 2014年2月28日 下午12:26:13
 * author: yangyd
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {
	
	/**
	 * spring容器
	 */
	private static ApplicationContext applicationContext;

	/* (non-Javadoc)
	 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
	 */
	@SuppressWarnings("static-access")
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}
	
	/**
	 * 从spring容器中获取某个特定名字的bean
	 * @param beanName
	 * @return
	 */
	public static Object getBean(String beanName){
        return applicationContext.getBean(beanName);
    }
	
	/**
	 * 从spring的消息机制中获取消息
	 * @param code 消息code
	 * @return
	 */
	public static String getMessage(String code) {
		return applicationContext.getMessage(code, null, null);
	}

	/**
	 * 从spring的消息机制中获取消息
	 * @param code 消息code
	 * @param params 消息参数
	 * @return
	 */
	public static String getMessage(String code, Object[] params) {
		return applicationContext.getMessage(code, params, null);
	}
}
